#include "key.h"
#include "led.h"
#include "delay.h"

int if_password_correct = 0;

void key_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;

    RCC_AHB1PeriphClockCmd(RCC_KEY_GROUP, ENABLE);

    GPIO_InitStruct.GPIO_Pin = KEY1_PIN | KEY2_PIN | KEY3_PIN | KEY4_PIN;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStruct.GPIO_Speed = GPIO_High_Speed;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(KEY_GROUP, &GPIO_InitStruct);
}

void key_led(void)
{
    if (GPIO_ReadInputDataBit(KEY_GROUP, KEY1_PIN) == 0)
    {
        delay(15);
        if (GPIO_ReadInputDataBit(KEY_GROUP, KEY1_PIN) == 0)
        {
            GPIO_ToggleBits(LED_GROUP, LED1_PIN);
            while (GPIO_ReadInputDataBit(KEY_GROUP, KEY1_PIN) == 0)
                ;
            delay(15);
        }
    }
    if (GPIO_ReadInputDataBit(KEY_GROUP, KEY2_PIN) == 0)
    {
        delay(15);
        if (GPIO_ReadInputDataBit(KEY_GROUP, KEY2_PIN) == 0)
        {
            GPIO_ToggleBits(LED_GROUP, LED2_PIN);
            while (GPIO_ReadInputDataBit(KEY_GROUP, KEY2_PIN) == 0)
                ;
            delay(15);
        }
    }
    if (GPIO_ReadInputDataBit(KEY_GROUP, KEY3_PIN) == 0)
    {
        delay(15);
        if (GPIO_ReadInputDataBit(KEY_GROUP, KEY3_PIN) == 0)
        {
            GPIO_ToggleBits(LED_GROUP, LED3_PIN);
            while (GPIO_ReadInputDataBit(KEY_GROUP, KEY3_PIN) == 0)
                ;
            delay(15);
        }
    }
    if (GPIO_ReadInputDataBit(KEY_GROUP, KEY4_PIN) == 0)
    {
        delay(15);
        if (GPIO_ReadInputDataBit(KEY_GROUP, KEY4_PIN) == 0)
        {
            GPIO_ToggleBits(LED_GROUP, LED4_PIN);
            while (GPIO_ReadInputDataBit(KEY_GROUP, KEY4_PIN) == 0)
                ;
            delay(15);
        }
    }
}

// 输入密码1010
void key_input_password(void)
{
    static int input[4] = {0, 0, 0, 0};
    static int i = 0;

    if (GPIO_ReadInputDataBit(KEY_GROUP, KEY1_PIN) == 0)
    {
        delay(15);
        if (GPIO_ReadInputDataBit(KEY_GROUP, KEY1_PIN) == 0)
        {
            input[i++] = 1;
            while (GPIO_ReadInputDataBit(KEY_GROUP, KEY1_PIN) == 0)
                ;
            delay(15);
        }
    }
    if (GPIO_ReadInputDataBit(KEY_GROUP, KEY2_PIN) == 0)
    {
        delay(15);
        if (GPIO_ReadInputDataBit(KEY_GROUP, KEY2_PIN) == 0)
        {
            input[i++] = 0;
            while (GPIO_ReadInputDataBit(KEY_GROUP, KEY2_PIN) == 0)
                ;
            delay(15);
        }
    }

    if (i == 4)
    {
        if (input[0] == 1 && input[1] == 0 && input[2] == 1 && input[3] == 0)
        {
            if_password_correct = 1;
            GPIO_ResetBits(LED_GROUP, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN);
            delay(200);
            GPIO_SetBits(LED_GROUP, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN);
            delay(200);
            GPIO_ResetBits(LED_GROUP, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN);
            delay(200);
            GPIO_SetBits(LED_GROUP, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN);
            delay(200);
        }
        else
        {
            if_password_correct = 0;
            GPIO_ResetBits(LED_GROUP, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN);
            delay(2000);
            GPIO_SetBits(LED_GROUP, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN);
        }

        i = 0;
    }
}

void key_control(void)
{
    if (if_password_correct)
    {
        if (GPIO_ReadInputDataBit(KEY_GROUP, KEY3_PIN) == 0)
        {
            delay(15);
            if (GPIO_ReadInputDataBit(KEY_GROUP, KEY3_PIN) == 0)
            {
                GPIO_ToggleBits(LED_GROUP, LED3_PIN);
                while (GPIO_ReadInputDataBit(KEY_GROUP, KEY3_PIN) == 0)
                    ;
                delay(15);
            }
        }
        if (GPIO_ReadInputDataBit(KEY_GROUP, KEY4_PIN) == 0)
        {
            delay(15);
            if (GPIO_ReadInputDataBit(KEY_GROUP, KEY4_PIN) == 0)
            {
                GPIO_ToggleBits(LED_GROUP, LED4_PIN);
                while (GPIO_ReadInputDataBit(KEY_GROUP, KEY4_PIN) == 0)
                    ;
                delay(15);
            }
        }
    }
}
